# encoding: utf-8
# 项目名称： 常用设计模式
# @File  : 007-单例模式.py
# @Author: sun hao
# @Desc : 
# @Date  :   16:12 2024/10/29


class Singleton:
    _instance = None

    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super(Singleton, cls).__new__(cls)
            cls._instance.initialized = False  # 初始化标志
        return cls._instance


class MyClass(Singleton):
    def __init__(self, a):
        if not self.initialized:
            self.a = a
            self.initialized = True


a1 = MyClass(100)
a2 = MyClass(200)
print(a1.a)  # 输出: 100
print(a2.a)  # 输出: 100
print(id(a1), id(a2))  # 输出: 相同的内存地址
